<?php
namespace Kxdrmobile\Controller;
use Think\Controller;
class IndexController extends ExtendController {
	public function index(){
		$EVENT = D("Kxdr/EventView");//10分钟读取一次更新数据
		$events = $EVENT->where("`event`.`sid`=".$this->sid." AND `event`.`status`=1")->getField("id,title,pic_url,gifting_time,game_name,game_status");
		// ->cache(true,600)

		$GUESS = D("Guess");
		$GUESS->sid=$this->sid;
		$GUESS->uid=$this->uid;
		$guess = $GUESS->saveSoccerDiff(); //比较猜分和比赛结果并保存记录，取出的是分差和猜分ID。只比较已结束的比赛

		if($GUESS->nengli_inc>0){
			$nengli_inc = $GUESS->nengli_inc;
			$this->user["nengli"]=$this->user["nengli"] + $nengli_inc;
			S($this->mixednick,$this->user);
		}
		$this->user["nengli"] = $this->user["nengli"]?$this->user["nengli"]:0;
		
		$USR = D("User");
		$USR->sid=$this->sid;
		$USR->uid=$this->uid;
		$USR->user_score=$this->user["nengli"];
		$user_title = $USR->getUserTitle();
		$user_jifen = $USR->getUserJifen();
		$this->user["user_title"]=$user_title;
		S($this->mixednick,$this->user);
		
		// S($this->sid.$this->uid."newgifts",null);
		
		// $newgifts = S($this->sid.$this->uid."newgifts"); //行176清除。读取缓存中的奖品。除非重设猜分数据（即清空缓存奖品），否则不再判断是否获得奖品
		if(!$newgifts)
		{
			$GIFT = D("Gifts");
			$GIFT->sid = $this->sid;
			$GIFT->uid = $this->uid;
			$newgifts = $GIFT->getWinGiftsByGuessDiff(); //使用数据库中已记录的猜分数据判断奖品
			S($this->sid.$this->uid."newgifts",$newgifts);
		}
		
		$newgifts["count"]=count($newgifts);
		$newgifts["count"]=$newgifts["count"]<100?$newgifts["count"]:"...";
		
		$this->assign("user_jifen",$user_jifen);
		$this->assign("user_title",$user_title);
		$this->assign("nengli_inc",$nengli_inc);
		$this->assign("gifts",$newgifts);
		$this->assign("events",$events);
		$this->assign("mixednick",$this->mixednick);
		$this->assign("ts",time());
		$this->display();
	}
	public function event(){
		$id = I("id");
		$EVENT = D("Kxdr/EventView");
		$GFT = D("Kxdr/GiftView");
		$PLR = D("Kxdr/EventPlayerView");
		
		$EVENT->sid=$this->sid;
		$EVENT->id=$id;
		$event = $EVENT->getEventView();
		
		$PLR->sid=$this->sid;
		$PLR->eid=$id;
		$PLR->uid=$this->uid;
		$players = $PLR->getEventPlayerView();

		$GFT->eid=$id;
		$GFT->sid=$this->sid;
		$GFT->getEventGifts();
		$gifts["gifts"]=$GFT->gifts;
		$gifts["gift_quant"]=$GFT->gift_quant;
		$gifts["gift_type"]=count($GFT->gifts);
		$gifts["gift_ids"]=array_merge($GFT->gift_ids);
		$gifts["gift_names"]=$GFT->gift_names;
		
		$this->assign("eid",$id);
		$this->assign("players",$players);
		$this->assign("gifts",$gifts);
		$this->assign("event",$event);
		$this->display();
	}
	public function mb(){
		$this->display();
	}
	public function guess(){
		$value = I("value");
		$guess_type = I("guess_type");
		$eid = I("eid");
		$sid = I("sid");
		$player_id = I("player"); //队伍的直接ID
		$event_player = I("event_player");//活动中队伍的ID
		$reset = I("reset");
		$guess_id = I("guess_id");
		$is_guessed = I("is_guessed");
		
		$EVENT = D("Kxdr/EventView");
		$GUESS = D("UserGuess");
		
		if($eid){
			$EVENT->sid=$this->sid;
			$EVENT->id=$eid;
			$EVENT->fields="id,start_time,time_out,time_out_type,multi_no,multi_yes";
			$event = $EVENT->getEventView();
			if($event["is_vaild"]<=0){
				$reda["data"]=$event;
				$reda["hasError"]=true;
				$reda["info"]="预测时间已截止，无法修改预测数据";
				$this->ajaxReturn($reda);
				die();
			}
			$type = Array("yes","no","only_y","only_n");
			if(in_array($guess_type,$type) && !$is_guessed){ //如果是已猜测，则肯定是删除动作，不予判断
				$guess = $GUESS->where("`sid`=".$sid." AND `eid`=".$eid." AND `user_id`=".$this->uid." AND (`guess_type` = 'yes' OR `guess_type` = 'no' OR `guess_type` = 'only_y' OR `guess_type` = 'only_n' )")->getField("`id`,`guess_type`");
				$multi_yes=0;
				$multi_no=0;
				foreach($guess as $gue){
					switch($gue){
						case "yes" : $multi_yes++;break;
						case "only_y" : $multi_yes++;break;
						case "no" : $multi_no++;break;
						case "only_n" : $multi_no++;break;
					}
				}
				if(($guess_type=="yes" || $guess_type=="only_y") && $multi_yes>=$event["multi_yes"] && $event["multi_yes"]>0 ){
					$info = "最多猜".$event["multi_yes"]."个是，您已经猜了".$event["multi_yes"]."个是";
				}
				if(($guess_type=="no" || $guess_type=="only_n") && $multi_no>=$event["multi_no"] && $event["multi_no"]>0){
					$info = "最多猜".$event["multi_no"]."个否，您已经猜了".$event["multi_no"]."个否";
				}
				if($info){
					$reda["data"]=$event;
					$reda["hasError"]=true;
					$reda["info"]=$info;
					$this->ajaxReturn($reda);
					die();
				}
			}
		}
		if($this->uid){
			
			$data["user_id"]=$this->uid;
			$data["eid"]=$eid;
			$data["sid"]=$sid;
			$data["guess_value"]=$value;
			$data["guess_type"]=$guess_type;
			$data["mixed_nick"]=$this->mixednick;
			$data["player_id"]=$player_id;
			$data["epid"]=$event_player;
			$data["guess_time"]=date("Y-m-d H:i:s");
			$data["status"]=1;
			
			
			if($is_guessed){
				
				$gswh["sid"]=$sid;
				$gswh["eid"]=$eid;
				$gswh["user_id"]=$this->uid;
				$gswh["epid"]=$event_player;
				
				$reda["hasError"]=!$GUESS->where($gswh)->delete();
				$reda["del"]=1;
				$reda["info"]="删除出错！";
				$guess_type_info = Array("yes"=>"√我猜是的","no"=>"×我猜不是","only_y"=>"√我猜是的","only_n"=>"×我猜不是");
				$data["guess_type_info"] = $guess_type_info[$data["guess_type"]];
				$data["id"]=$guess_id;
				$reda["guess"]=$data;
				$this->ajaxReturn($reda);
				die();
			}
			$gswh["sid"]=$sid;
			$gswh["eid"]=$eid;
			$gswh["user_id"]=$this->uid;
			$gswh["epid"]=$event_player;
			$guess = current($GUESS->where($gswh)->getField("`id`,`guess_value`,`guess_type`,`player_id`,`guess_time`"));
			
			if($guess){
				if($reset){
					$GUESS->where("`id`=".$guess_id)->save($data);
					$data["id"]=$guess_id;
					$reda["hasError"]=false;
					$reda["guess"]=$data;
					$reda["info"]="已经修改 edited";
				}else{
					$reda["hasError"]=true;
					$reda["guess"]=$guess;
					$reda["info"]="预测过啦！如果需要修改预测，请先取消";
				}
			}else{
				$w["sid"]=$sid;
				$w["eid"]=$eid;
				$w["user_id"]=$this->uid;
				$ct = $GUESS->cache(true)->where($gswh)->limit(1)->getField("id");
				if(!$ct){
					$USR = M("User");
					$u["id"]=$this->uid;
					$u["sid"]=$this->sid;
					$USR->where($u)->setInc("joined_events");
				}
				$re = $GUESS->add($data);
				if($re){
					$data["id"]=$re;
					$reda["guess"]=$data;
					$reda["hasError"]=false;
					$reda["info"]="成功预测！";
				}
			}
			S($this->sid.$this->uid."newgifts",null); //清理缓存中的奖品，等待再次判断是否中奖
		}else{
			$reda["hasError"]=true;
			$reda["error"]="没有检测到用户信息";
		}
		$reda["del"]=$reda["del"]?$reda["del"]:0;
		$this->ajaxReturn($reda);
	}
}